package com.authstr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.authstr.entity.OperationSetting;
import com.authstr.serve.DataSync;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@Controller
public class Manage {
	@RequestMapping("/onlyDataSync")
	public Map onlyDataSync(OperationSetting setting) throws Exception {
		Map map=success();
		JSONObject jo= (JSONObject) JSONObject.toJSON(setting);
		map.put("data", DataSync.dataSync(setting.createNewConn(),setting.createOldConn(),setting));
		map.put("msg","生成成功");
		return map;
	}

	@RequestMapping("/settingToJson")
	public Map settingToJson(OperationSetting setting) throws Exception {
		Map map=success();
		List<OperationSetting> aa=new ArrayList<>();
		aa.add(setting);
		aa.add(setting);
		JSONObject jo= (JSONObject) JSONObject.toJSON(setting);
		map.put("data", jo.toJSONString());
		map.put("msg","转换成功");
		return map;
	}
	@RequestMapping("/getSettingForJson")
	public Map getSettingForJson(String json) throws Exception {
		Map map=success();
		OperationSetting setting = JSON.parseObject(json, OperationSetting.class);
		JSONObject jo= (JSONObject) JSONObject.toJSON(setting);
		map.put("setting",setting);
		map.put("msg","加载成功");
		return map;
	}
	@RequestMapping("/onlyDataSyncForJson")
	public Map settingToJson(String json) throws Exception {
		Map map=success();
		OperationSetting setting = JSON.parseObject(json, OperationSetting.class);
		JSONObject jo= (JSONObject) JSONObject.toJSON(setting);
		map.put("setting",setting);
		map.put("data", DataSync.dataSync(setting.createNewConn(),setting.createOldConn(),setting));
		map.put("msg","执行成功");
		return map;
	}

	@RequestMapping("/manyDataSyncForJson")
	public Map manyDataSyncForJson(String jsonArray) throws Exception {
		Map map=success();
		List<OperationSetting> settingArray = JSON.parseObject(jsonArray,new TypeReference<List<OperationSetting>>(){});
		List<String> res=new ArrayList<>();
		for(int i=0;i<settingArray.size();i++){
			OperationSetting setting=settingArray.get(i);
			res.addAll( DataSync.dataSync(setting.createNewConn(),setting.createOldConn(),setting));
			res.add("");
			res.add("");
			res.add("");
			res.add("");
			res.add("");
			res.add("");
		}
		map.put("data", res);
		map.put("msg","批量执行成功");
		return map;
	}






	@ExceptionHandler({Exception.class})
	@ResponseBody
	public Map exceptionHandler(Exception ex) {
		Map model = new HashMap();
		if (ex instanceof Exception) {
			model.put("code", "-1");
			model.put("msg", null!=ex.getMessage()?ex.getMessage():"未知异常,请查看cmd");
		}
		ex.printStackTrace();
		return model;
	}

	public Map success() {
		Map map = new HashMap();
		map.put("code", "1");
		return map;
	}


}
